package com.bdqn.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bdqn.dto.RealEstateDTO;
import com.bdqn.mapper.UserMapper;
import com.bdqn.model.Users;
import com.bdqn.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Service("userService")
public class UserServiceImpl implements UserService{
	@Autowired
	private UserMapper userMapper;
	
	@Override
	public Users login(Users user) {
		
		Users u = userMapper.login(user);
		
		return u;
	}

	@Override
	public int update(Users user) {
		return userMapper.update(user);
	}

	//查找所有登陆用户
	@Override
	public List<Users> queryUserList() {
		List<Users> l = userMapper.queryUserList();
		return  l ;
	}


	@Override
	public boolean updateUser(Users user) {
		String loginAccount=user.getLoginAccount();
		String loginName=user.getLoginName();
		String LoginPassword=user.getLoginPassword();
		String Role=user.getRole();
		String status=user.getStatus();
		
		Date day=new Date();
		SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
		String createDateTime=df.format(day);
		int row=userMapper.updateUser(loginAccount,loginName,LoginPassword,Role,status,createDateTime);
		if(row == 1) {
			return true;
		}
		return false;
	}

	@Override
	public boolean addUserData(Users user) {
		String loginAccount=user.getLoginAccount();
		String loginName=user.getLoginName();
		String LoginPassword=user.getLoginPassword();
		String Role=user.getRole();
		String status=user.getStatus();
		Date day=new Date();
		SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
		String createDateTime=df.format(day);
		
		int row =userMapper.addUserData(loginAccount,loginName,LoginPassword,Role,status,createDateTime);
		if(row == 1) {
			return true;
		}
		return false;
	}

	@Override
	public boolean deleteUserData(String loginAccount) {
		int row=userMapper.deleteUserData(loginAccount);
		if(row == 1) {
			return true;
		}
		return false;
	}

	@Override
	public PageInfo<Users> listPage(RealEstateDTO dto) {
		PageHelper.startPage(dto.getPageNum(), 3);
		List<Users> l = userMapper.queryUserList();
		PageInfo<Users> pageInfo = new PageInfo<Users>(l);
		pageInfo.setList(l);
		return pageInfo;
		
	}
	

}
